import { sys } from './../../../../creator.d';
import vv from "../../Common/vv";
import GameCtr from "../../Controller/GameCtr";
import GameDataInstance from '../../Common/GameDataInstance';
import GameData from '../../Common/GameData';

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    

    //返回到首页
    @property(cc.Node)
    home:cc.Node = null

    //分数
    @property(cc.Node)
    score:cc.Node = null

    //在玩一局
    @property(cc.Node)
    replay:cc.Node = null
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}


      //复活
      Revive()
      {
        GameDataInstance.Instance.videoid.show()
        .then(() => {
            console.log('广告显示成功');
        })
        .catch(err => {
            console.log('广告组件出现问题', err);
            vv.showTip('广告组件出现问题，暂时无法复活！')
            // 可以手动加载一次
            GameDataInstance.Instance.videoid.load()
                .then(() => {
                    console.log('手动加载成功');
                });
        });
          
         // GameCtr.ReviveGameSettingCtr()
      }

    start () {
        
        if(GameDataInstance.Instance.videoid!=null)
        {
             GameDataInstance.Instance.videoid.onClose(res => {
            if (res.isEnded) {
                // 给予奖励
                cc.director.loadScene('fkxxl')
            }
        });
        }

       

        // console.log('上边距22',GameDataInstance.Instance.menuPosition.top)
        // this.home.getComponent(cc.Widget).top = GameDataInstance.Instance.menuPosition.top*5
       // vv.share()
        this.score.getComponent(cc.RichText).string = GameCtr.score.toString()

        cc.sys.localStorage.setItem('maxscore',GameCtr.score)

        if(GameDataInstance.Instance.MaxScore<GameCtr.score)
        {
          
            let jiafen = (Number)(GameCtr.score) - (Number)(GameDataInstance.Instance.MaxScore)
            console.log('更新的积分数',jiafen)
            GameDataInstance.Instance.MaxScore = GameCtr.score
            vv.sendPostHttpRequest(GameDataInstance.Instance.updatejifen,{'openid':GameDataInstance.Instance.openid,'type':242,'no':jiafen},(res)=>
            {
              console.log('更新用户分数成功',res)
            })
        }

          vv.btnClick(this.home,()=>
        {
            cc.director.loadScene('Start')
        })


        this.replay.on('touchend',()=>
        {
            GameData.level = 0
            GameCtr.exp = 0
            GameCtr.score = 0
            cc.director.loadScene('fkxxl')
        })
    }

    // update (dt) {}
}
